package com.example.demo.utli;

import org.apache.commons.codec.digest.DigestUtils;
//import org.apache.tomcat.util.security.MD5Encoder;

public class Ove {

    private static int offset = 5 ;

    private static String saltFront ;
    private static String saltAfter ;
    private static String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static String salt = "salt";



    public static String encryption(String str,String userName) {
//        char[] m = model.toCharArray();
//        for (int j = 0; j < 6; j++) {
//            char c = m[(int) (Math.random() * 36)];
//
//            // 保证六位随机数之间没有重复的
//            if (saltFront.contains(String.valueOf(c))) {
//                j--;
//                continue;
//            }
//            saltFront = saltFront + c;
//
//            char c2 = m[(int) (Math.random() * 36)];
//            if (saltAfter.contains(String.valueOf(c2))) {
//                j--;
//                continue;
//            }
//            saltAfter = saltAfter + c;
//        }
        str = salt + userName + str  ;

        int a[] = new int[100];
        char ch[] = new char[100];
        ch = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            a[i] = (int) ch[i];
            a[i] = a[i] + offset;
            ch[i] = (char) a[i];
        }
        return DigestUtils.md5Hex(new String(ch));
    }

    public static String decrypt(String str) {
        int a[] = new int[100];
        char ch[] = new char[100];
        ch = str.toCharArray();
        for (int i = 0; i < str.length(); i++) {
            a[i] = (int) ch[i];
            a[i] = a[i] - offset;
            ch[i] = (char) a[i];
        }
        String s = new String(ch);
        return s.substring(6,s.length() - 6);
    }

}